<template xmlns:v-action="http://www.w3.org/1999/xhtml">
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="活动标题">
              <a-input v-model="queryParam.title" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24" v-action:other>
            <a-form-item label="商圈">
              <a-select allowClear v-model="queryParam.companyId" v-decorator="[ 'companyId']">
                <a-select-option v-for="item in companys" :key="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.model.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :indentSize="40"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="$refs.model3.Send(record.id)">发送</a>
           <a-divider type="vertical"/>
           <a v-action:update @click="$refs.model2.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a v-action:delete @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>

    <Add ref="model" @ok="handleOk"></Add>
    <Edit ref="model2" @ok="handleOk"></Edit>
    <Send ref="model3" @ok="handleOk"></Send>
  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import {selectPage,del} from '@/api/message/activity'
  import { selectAllCompany } from '@/api/system/company'
  import Edit from './Edit'
  import Add from './Add'
  import Send from './Send'

  export default {
    components: {
      STable,
      Edit,
      Add,
      Send
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        companys:[],
        // 表头
        columns: [
          {
            title: '活动标题',
            dataIndex: 'title'
          },
          {
            title: '商圈名称',
            dataIndex: 'companyName'
          },
          {
            title: '活动简介',
            dataIndex: 'introduction'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '200px',
            scopedSlots: {customRender: 'action'}
          }

        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created() {
      selectAllCompany().then(res => {
        this.companys = res.result
      })
    },
    //--############  data() ###############-->
    //--############  methods() ###############-->
    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.title} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      }
    }
    //--############  methods() ###############-->
  }
</script>